package com.lxf.springcloud.controller;

import com.lxf.springcloud.pojo.Dept;
import com.lxf.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lxf
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询抛出异常
     *
     * @param id id
     * @return Dept
     */
    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrix")
    public Dept get(@PathVariable Long id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("这个id=>" + id + ",不存在该用户，或信息无法找到~");
        }
        return dept;
    }

    /**
     * 根据id查询备选方案(熔断)
     *
     * @param id id
     * @return Dept
     */
    public Dept hystrix(@PathVariable Long id) {
        Dept dept = new Dept();
        dept.setDeptno(id);
        dept.setDeptname("这个id=>" + id + ",没有对应的信息,null---@Hystrix~");
        dept.setDb_source("在MySQL中没有这个数据库");
        return dept;
    }
}
